package com.chookss.video

import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.TextView
import com.chookss.R
import com.chookss.tools.GlideUtils
import com.chookss.tools.Utils
import com.chookss.video.entity.VideoComment

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/21 9:46
 * Note:
 */
open class VideoCommentsAdapter : BaseExpandableListAdapter() {
    companion object {
        val ChildTypeComment = 0
        val ChildTypePlaceHolder = 1
    }
    val groups = ArrayList<GroupItem>()
    var isLoadingGroupId:String? = null//正在加载二级评论的组
    var onItemBtnClick:OnItemBtnClick? = null
    private val likeListener = View.OnClickListener {
        onItemBtnClick?.onCommentClick(it, it.tag as VideoComment)
    }

    private fun updateCommentView(view: View, comment: VideoComment) {
        view.findViewById<TextView>(R.id.tvName).apply {
            text = comment.fromEmployeeName
        }
        var lvlNo:Int= 1
        if(!Utils.isNull(comment.lvlNo)){
            lvlNo= comment.lvlNo.replace(" ", "") .toInt()
        }
        if (lvlNo > 2 && !TextUtils.isEmpty(comment.toEmployeeName)) {
            view.findViewById<TextView>(R.id.tvName2).visibility = View.VISIBLE
            view.findViewById<TextView>(R.id.tvName2).text = comment.toEmployeeName
        }else{
            view.findViewById<TextView>(R.id.tvName2).visibility =  View.GONE
        }
        view.findViewById<TextView>(R.id.tvContent).text = comment.videoComment
        view.findViewById<TextView>(R.id.tvZan).apply {
            var commentLikeCounts:Int=0
            if(Utils.isNull( comment.commentLikeCounts)){
                commentLikeCounts =0
            }else{
                commentLikeCounts =comment.commentLikeCounts.toInt()
            }

            if("1".equals(comment.isLike)&&commentLikeCounts>0){
                isSelected=true
            }else{
                isSelected=false
            }
            text =commentLikeCounts.toString()
            tag = comment
            setOnClickListener(likeListener)
        }

        view.findViewById<TextView>(R.id.tvTime).text = Utils.getAccurateDate(comment.createTime)
        GlideUtils.loadAvatar(view.context,  comment.headPhotoThumbUrl, view.findViewById(R.id.ivAvatar));
    }

    override fun getGroupCount() = groups.size

    override fun getChildrenCount(groupPosition: Int): Int {
        var p = getGroup(groupPosition)
        var haveChild:Int= 0
        if(!Utils.isNull(p.comment.haveChild)){
            haveChild= p.comment.haveChild.toInt()
        }
        return if (haveChild> 0) {
            if (p.showChildren) {
                p.children.size + 1
            } else {
                1
            }
        } else {
            0;
        }
    }

    override fun getGroup(groupPosition: Int) = groups[groupPosition]

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        var p = getGroup(groupPosition)
        if (p.showChildren && childPosition < p.children.size) {
            return p.children[childPosition]
        } else {
            return PlaceHolder()
        }
    }

    override fun getGroupId(groupPosition: Int) = 0L

    override fun getChildId(groupPosition: Int, childPosition: Int) = 0L

    override fun hasStableIds() = false

    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup): View {
        var view = convertView
                ?: View.inflate(parent.context, R.layout.item_video_comment_group, null)
        updateCommentView(view, getGroup(groupPosition).comment)
        return view
    }

    override fun getChildTypeCount() = 2
    override fun getChildType(groupPosition: Int, childPosition: Int): Int {
        var p = getGroup(groupPosition)
        if (p.showChildren && childPosition < p.children.size) {
            return ChildTypeComment
        } else {
            return ChildTypePlaceHolder
        }
    }

    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup): View {
        var viewType = getChildType(groupPosition, childPosition)
        if (viewType == ChildTypeComment) {
            var view = convertView
                    ?: View.inflate(parent.context, R.layout.item_video_comment_child, null)
            updateCommentView(view, getChild(groupPosition, childPosition) as VideoComment)
            return view
        } else {
            var view = convertView
                    ?: View.inflate(parent.context, R.layout.item_video_comment_placeholder, null)
            var groupItem = getGroup(groupPosition)
            var tv = view.findViewById<TextView>(R.id.tvContent)
            var state = groupStateOf(groupItem)
            if(state.title.contains("查看")||state.title.contains("展开")){
                tv.text = "展开"+groupItem.comment.haveChild+"条回复"
                tv.isSelected=false
            }else{
                tv.text = state.title
                tv.isSelected=true
            }

            return view
        }
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int) = true

    data class GroupItem(
            var comment: VideoComment,
            val children: ArrayList<VideoComment> = ArrayList(),
            var showChildren: Boolean = true,
    )
    enum class GroupState(var title: String, var clickToLoad: Boolean) {
        IsLoading("正在加载中", false),
        AtEndToLoadMore("查看更多", true),
        AtBeginToLoadMore("展开", true),
        AtEnd("收起", false),
        AtBegin("展开", false),
    }

    fun groupStateOf(g: GroupItem): GroupState {
        var haveChild:Int= 0
        if(!Utils.isNull(g.comment.haveChild)){
            haveChild= g.comment.haveChild.toInt()
        }
        if (g.comment.commentCode == isLoadingGroupId) {
            return GroupState.IsLoading
        } else if (g.showChildren) {
            if (haveChild <= g.children.size) {
                return GroupState.AtEnd
            } else {
                return GroupState.AtEndToLoadMore
            }
        } else {
            if (haveChild <= g.children.size) {
                return GroupState.AtBegin
            } else {
                return GroupState.AtBeginToLoadMore
            }
        }
    }

    interface OnItemBtnClick {
        fun onCommentClick(view: View, entity: VideoComment)
    }
    class PlaceHolder()
}